package com.example.haosiji.activity;

import com.example.haosiji.R;
import com.example.haosiji.app.haosijiApp;
import com.example.haosiji.listview.OrderListView;

import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivity extends ActivityGroup {
	
	private RadioGroup rgManager;
	int index = 0;
	private long clickTime = 0;
	private haosijiApp mApp = null;
	
	private OrderListView orderListView;
	private LinearLayout layout_map_list;
	private TextView tv_tittle;
	private ImageView splite;
	private TextView address;
	public TextView isEmpty;
	private View mainView;
	private RelativeLayout mNaviBar;
	
	private static LocalActivityManager mLocalActivityManager;
	private static ViewFlipper viewFlipper;
	
	public Button btn_driverlist;
	public Button btn_map;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
		orderListView = (OrderListView)findViewById(R.id.order_listview);
	}
	
	private void initView(){
		mApp = ((haosijiApp) getApplication());
		mApp.addActivity(this);
		
		rgManager = (RadioGroup)findViewById(R.id.radioGroup);
		setRadioButtons(rgManager);
		
		layout_map_list = ((LinearLayout)findViewById(R.id.layout_map_list));
		btn_map = (Button)findViewById(R.id.btn_map);
		btn_driverlist = (Button)findViewById(R.id.btn_driver);
		
		splite = (ImageView)findViewById(R.id.split);
		address = (TextView)findViewById(R.id.address);
		isEmpty = (TextView)findViewById(R.id.isEmpty);
		tv_tittle = ((TextView)findViewById(R.id.title));
		
		viewFlipper = ((ViewFlipper) findViewById(R.id.homeview_control));
		mLocalActivityManager = getLocalActivityManager();
		
		if(viewFlipper != null){
			viewFlipper.removeAllViews();
		}
		toDriverMap();
	}
	
	/*viewFlipper的切换*/
	public static void switchActivity(String paramString, Intent paramIntent){
	    View localView = mLocalActivityManager.startActivity(paramString, paramIntent).getDecorView();
	    try
	    {
	    viewFlipper.addView(localView);	  
	      int j = viewFlipper.indexOfChild(localView);
	      viewFlipper.setDisplayedChild(j);   
		  return;
	    }
	    catch (Exception localException)
	    {
	    	int i = viewFlipper.indexOfChild(localView);
	    	viewFlipper.setDisplayedChild(i);   
			return;
	    }    
	    
	}
	/*切换到价格*/
	private void toPrice() {
		Intent localIntent = new Intent(this, PriceMenu.class);
		switchActivity("toPrice", localIntent);
	}
	
	private void toDriverMap() {
		Intent localIntent = new Intent(this, NearBy.class);
		switchActivity("toDriverMap", localIntent);
	}
	
	private void toMyOrder() {
		Intent localIntent = new Intent(this, myOrder.class);
		switchActivity("toMyOrder", localIntent);
	}
	
	private void toMore() {
		Intent localIntent = new Intent(this, moreActivity.class);
		switchActivity("toMore", localIntent);
	}
	
	/*
	 * 底部tab键的切换
	 * */
	private void setRadioButtons(RadioGroup paramRadionGroup){	
		paramRadionGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				
				switch(arg1){
				case R.id.tabbar_search_drivers:
					if (index != 0) {
						layout_map_list.setVisibility(View.VISIBLE);
						toDriverMap();
						index = 0;
					}
					break;
				case R.id.tabbar_my_drivers:
					if (index != 1) {
						tv_tittle.setText("我的订单");
						splite.setVisibility(View.VISIBLE);
						address.setVisibility(View.INVISIBLE); 
						
						isEmpty.setText("清空");
						isEmpty.setVisibility(View.VISIBLE);
						layout_map_list.setVisibility(View.INVISIBLE); 
						toMyOrder();
						index = 1;
					}
					break;
				case R.id.tabbar_price:
					if (index != 2) {
						
						tv_tittle.setText("价格表");
						splite.setVisibility(View.VISIBLE);
						isEmpty.setVisibility(View.INVISIBLE); 
						
						address.setVisibility(View.VISIBLE);
						address.setText("上海");
						layout_map_list.setVisibility(View.INVISIBLE);
						toPrice(); 
						index = 2;
					}
					break;
				case R.id.tabbar_share:
					if (index != 3) {
						tv_tittle.setText("更多信息");
						splite.setVisibility(View.INVISIBLE);
						address.setVisibility(View.INVISIBLE);
						isEmpty.setVisibility(View.INVISIBLE); 
						layout_map_list.setVisibility(View.INVISIBLE);
						toMore();
						index = 3;
					}
					break;		
				
				}
				
				((RadioButton) arg0.getChildAt(index)).setChecked(true);
				
			}
					
		});		
	}

	protected void onDestroy() {
		super.onDestroy();
		mApp.removeActivity(this);
	}
	
	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent){
		if(paramInt == KeyEvent.KEYCODE_BACK){
			
			if(System.currentTimeMillis() - clickTime > 2000){
				Toast.makeText(this, "再按一次退出应用！", Toast.LENGTH_SHORT).show();
				clickTime = System.currentTimeMillis();
			}else{
				mApp.exit();
			}
			
		}
		
		return false;
	}
	
	
	
}
